<?
  class Tab
  {
	var $aTabs;
	var $nSelected;
	var $cClass;
	var $cClassName ;
	var $nStyle;
	var $cUrlBase;
	var $cParameterName;
	var $bRememberSession = false;
	var $bMergeParameter = true;
	var $cTarget = 'body_main';

	function getTabUrl( $cValue)
	{
		$cUrlBase = replaceString( $this->cParameterName , $cValue, $this->cUrlBase );
		if ( $this->aTabs[$cValue][3] ) $cUrlBase .=  '&amp;'  .$this->aTabs[$cValue][3] ;
		return $cUrlBase ;
	}
	function setClass( $cClass)
	{
		$this->cClass= $cClass;
	}
	function setTarget( $cTarget)
	{
		$this->cTarget= $cTarget;
	}
	function setMergeParameter( $bOn = true)
	{
		$this->bMergeParameter = $bOn;
	}
	function setRememberSession( $bOn = true)
	{
		$this->bRememberSession = $bOn;
	}
	function setUrl( $cUrlBase )
	{
		$this->cUrlBase = $cUrlBase;
	}
	function setValue( $cValue )
	{
		global $_SESSION;
		$this->nSelected = $cValue;
		if ( $this->bRememberSession ) { $_SESSION[$this->cParameterName] = $cValue; }
	}
	function setDefault( $cDefaultValue )
	{
		if ( !$this->nSelected ) $this->setValue( $cDefaultValue );
	}
	function setStyle( $nStyle )
	{
		$this->nStyle = $nStyle;
		$this->setValue( getParam($this->cParameterName) );
	}
	function Tab( $cClassName , $nStyle = TAB_TOP, $cParameterName = '', $bRememberSession = false )
	{
		global $_SESSION;
		$this->cClassName = $cClassName ;
		$this->bRememberSession = $bRememberSession;
		$this->cParameterName = ( $cParameterName == '' ) ? PARAM_TAB . '_'. $nStyle : $cParameterName;
		// Si remember session && tiene valor => lo setea
		if ( $this->bRememberSession && array_key_exists($this->cParameterName, $_SESSION) )
		{
			$this->nSelected = $_SESSION[$this->cParameterName];
		}
		$this->setStyle ( $nStyle );
		$aStyle = array ( 'top', 'bottom', 'left', 'right' );
		$this->cClass = 'tabs_' . $aStyle[$this->nStyle-1];
		
		$this->cUrlBase = getThisUrl();		
		if ( MODO_AJAX == true)
		{
			$cUrlBase = replaceString( PARAM_REFRESH, "class", $this->cUrlBase);
			$cUrlBase = replaceString( PARAM_CLASS, $this->cClassName, $cUrlBase);
			$this->cUrlBase = $cUrlBase ;
		}
	}

	function setMatrix( $aDatos , $bTodas = true)
	{
		if ( count($aDatos) > 1 && $bTodas )	$this->addTab( 'Todas', ' ');
		foreach( $aDatos as $aDato )
		{
			$this->addTab( $aDato[1], $aDato[0], $aDato[2], $aDato[3], $aDato[4] );
		}
	}
	function setMatrixMultilanguage( $aDatos , $bTodas = true, $cLanguage = 'es' )
	{
		if ( count($aDatos) > 1 && $bTodas )	$this->addTab( 'Todas', ' ');
		foreach( $aDatos as $aDato )
		{
			$this->addTab( extractLanguage($aDato[1], $cLanguage) , $aDato[0], $aDato[2], $aDato[3], $aDato[4] );
		}
	}
	function addTab ( $cName, $cValue = '', $cAccessKey = '', $cParametros = '', $cColor = '' )
	{
	  if ( $cValue  == '' ) $cValue = count($this->aTabs) + 1;
	  $this->aTabs[$cValue] = array ( $cValue , $cName, $cAccessKey, $cParametros , $cColor) ;
	}

	function getValue()
	{
		if ( array_key_exists( $this->nSelected , $this->aTabs)  )   return $this->aTabs[$this->nSelected][0];
		// Si no existe devuelve el primero
		foreach( $this->aTabs as $aTab )
			return $aTab[0];
	}

	function getName()
	{
	  return $this->aTabs[$this->nSelected ][1];
	}

	function make()
	{
		if ( ! is_array( $this->aTabs ) )
		{
			return false;
		}
		$cUrlBase = replaceString( $this->cParameterName , '', $this->cUrlBase );
	  // Arma el Principio de la Tabla
		
		echo "<div style='clear: none; overflow: auto'>";
		if ( $this->cClass )
		{
			echo "<ul class='" . $this->cClass . "'>";
		}else
		{
			echo "<ul>";
		}
		
	  // Cicla por cada Tab
		foreach ( $this->aTabs as $aTab )
		{
		  $cValue = $aTab[0];
		  $cName  = $aTab[1];
		  $cParametros  = $aTab[3];
		  // Si tiene AccessKey
		  if ( $cValue != $this->nSelected  && $aTab[2] )
		  {
			$nPos = strpos( $cName, $aTab[2] );
			if ( ! ( $nPos === false) )
			{
			  $cName = substr( $cName, 0, $nPos ) . '<u>' . substr( $cName, $nPos, strlen($aTab[2]) ) . '</u>' . substr( $cName, $nPos+ strlen($aTab[2]) ) ;
			  $cAccessKey = " accesskey='" . $aTab[2] . "' ";
			}
		  }
		  if ( $cValue == $this->nSelected )
		  {
			$cClase  = "tab_actual";
			$cTab = "$cName";
		  } else
		  {
			$cClase  = "tab_resto";

			$cUrlParametros =  $this->cParameterName . '=' .$cValue;
			//if ( $cParametros  ) $cUrlParametros .= '&' . $cParametros ;
			$cUrl = $cUrlBase . '&amp;' . $cUrlParametros . '&amp;' . $cParametros ;
			if ( MODO_AJAX == true  )
			{
				$cMergeFunction = ( $this->bMergeParameter ) ? "mergeParameterUrl( \"" . $this->cTarget . "\" , \"$cUrlParametros\", \"". $cName ."\")'; " :"";
			  $cHRef = " href='javascript:void(0);' onclick='send(\"$cUrl\",refreshElement, \"" . $this->cClassName . "\" ); $cMergeFunction return false;' ";
			} else
			{
			  $cHRef = " href='$cUrl' ";
			}
			// Agrega la Clase que Tiene el Tab
			$cTab = "<a $cAccessKey $cHRef>$cName</a>";
			// Si tiene color => setea
			$cColor = ( $aTab[4] != ''  ) ?  "style='background-color:#" . $aTab[4] . "'" : '';
		  }

			echo "<li ". $cColor ." class='". $cClase ."' align='center'>". $cTab ."</li>";

		}
	  // Agrega el final de la lista
	  echo "</ul>";
	  echo "</div>";
	}
  }
?>
